//
//  TitleButton.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/6/17.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit

class TitleButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    //通过xib/SB创建时调用
    required init?(coder aDecoder: NSCoder) {
        //系统对initWithCoder的默认实现时报一个致命错误
//        fatalError("init(coder:) has not been implemented")
        super .init(coder: aDecoder)
        
        setupUI()
    }
    
    func setupUI(){
        setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
        setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        setTitleColor(.darkGray, for: .normal)
        sizeToFit()
    }
    
    
    override func setTitle(_ title: String?, for state: UIControl.State) {
        //?? 用于判断前面的参数是否是nil，如果是nil就返回??后面的数据，如果不是nil那么??后面的语句不执行
        var title_text = title ?? ""
        title_text = title_text + "  "
        super.setTitle(title_text, for: state)
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //offset 设置控件的偏移位
//        titleLabel?.frame.offsetBy(dx: -imageView!.frame.size.width, dy: 0)
//        imageView?.frame.offsetBy(dx: titleLabel!.frame.width, dy: 0)
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.width
    }
}
